package com.ptu.service.impl;


import com.ptu.entity.Employee;
import com.ptu.entity.PageTools;
import com.ptu.mapper.EmployeeMapper;
import com.ptu.service.EmployeeService;
import com.ptu.util.DBUtil;

import java.util.List;

public class EmployeeServiceImpl implements EmployeeService {

    @Override
    public int addEmp(Employee emp) {
        EmployeeMapper mapper = DBUtil.getSqlSession().getMapper(EmployeeMapper.class);
        int insert = mapper.insert(emp);
        DBUtil.closeAll();
        return insert;
    }

    @Override
    public List<Employee> findEmpMore(String empid, Integer posid, Integer onduty) {
        EmployeeMapper mapper = DBUtil.getSqlSession().getMapper(EmployeeMapper.class);
        List<Employee> employees = mapper.selectEmpMore(empid, posid, onduty);
        DBUtil.closeAll();
        return employees;
    }

    @Override
    public int removeEmp(String empid) {
        EmployeeMapper mapper = DBUtil.getSqlSession().getMapper(EmployeeMapper.class);
        int i = mapper.delEmp(empid);
        DBUtil.closeAll();
        return i;
    }

    @Override
    public PageTools<Employee> findEmpAll(int pageSize, int pageNumber) {
        EmployeeMapper mapper = DBUtil.getSqlSession().getMapper(EmployeeMapper.class);

        PageTools<Employee> pageTools = new PageTools<>();
        pageTools.setPageNumber(pageNumber);
        pageTools.setPageSize(pageSize);

        int pageStart = pageSize*(pageNumber-1);
        List<Employee> employees = mapper.selectPage(pageSize, pageStart);
        int total = mapper.selectCount();
        pageTools.setTotal(total);
        pageTools.setList(employees);

        DBUtil.closeAll();
        return pageTools;
    }

    @Override
    public Employee findEmpByEmpid(String empid) {
        EmployeeMapper mapper = DBUtil.getSqlSession().getMapper(EmployeeMapper.class);
        List<Employee> emp = mapper.selectEmpMore(empid, null, null);
        DBUtil.closeAll();
        return emp.get(0);
    }

    @Override
    public int changeEmp(Employee emp) {
        EmployeeMapper mapper = DBUtil.getSqlSession().getMapper(EmployeeMapper.class);
        int update = mapper.update(emp);
        DBUtil.closeAll();
        return update;
    }

    @Override
    public List<Employee> findMgr() {
        EmployeeMapper mapper = DBUtil.getSqlSession().getMapper(EmployeeMapper.class);
        List<Employee> list = mapper.selectMgr();
        DBUtil.closeAll();
        return list;
    }

    @Override
    public int changePwd(String empid, String password) {
        EmployeeMapper mapper = DBUtil.getSqlSession().getMapper(EmployeeMapper.class);
        int result = mapper.updatePwd(empid, password);
        DBUtil.closeAll();
        return result;
    }

}
